package com.example.alipay.tss;

import cn.org.bjca.client.exceptions.*;

import cn.org.bjca.client.security.SecurityEngineDeal;
import org.junit.Before;
import org.junit.Test;

import javax.jws.soap.SOAPBinding;
import java.nio.charset.StandardCharsets;


public class Tss {

    SecurityEngineDeal sedTss = null;
    @Before
    public void init() throws ApplicationNotFoundException, InitException, SVSConnectException {
        SecurityEngineDeal.setProfilePath("C:\\BJCAROOT");

        sedTss = SecurityEngineDeal.getInstance("TSSDefault");
        if(sedTss!=null){
            System.out.println(sedTss);
            System.out.println("连接tss服务器成功");

        }else {
            System.out.println("连接tss服务器失败");
        }

    }

    /**
     * 创建时间戳请求  带证书
     * @throws ParameterTooLongException
     * @throws ParameterInvalidException
     * @throws SVSConnectException
     */
    @Test
    public void createTssequest() throws ParameterTooLongException, ParameterInvalidException, SVSConnectException {
        String inData = "Hello World";
        String tsReq = sedTss.createTSRequest(inData.getBytes(),true);
        System.out.println("时间戳请求："+tsReq);
        String result = sedTss.createTS(tsReq);
        System.out.println("时间戳响应"+result);
    }
    /**
     * 创建时间戳请求  不带证书
     * @throws ParameterTooLongException
     * @throws ParameterInvalidException
     * @throws SVSConnectException
     */
    @Test
    public void createTssequest2() throws ParameterTooLongException, ParameterInvalidException, SVSConnectException, ParameterOutRangeException {
        String inData = "Hello World";
        String tsReq = sedTss.createTSRequest(inData.getBytes(),true);
        System.out.println("时间戳请求："+tsReq);
        String result = sedTss.createTS(tsReq);
        System.out.println("时间戳响应"+result);
//        type =1 返回时间，type＝2 返回原文的 hash 值，type＝3 返回签名证书，type=4 返回
//        服务器证书序列号。
         result = "MIIEeAYKKoEcz1UGAQQCAqCCBGgwggRkAgEBMQ8wDQYJKoEcz1UBgxEBBQAwZgYLKoZIhvcNAQkQAQSgVwRVMFMCAQEGCCsGAQUFBwMIMC0GCSqBHM9VAYMRAQQgy93bjoQhsjSYSAVw19dTMFOKaIL139w7ZBFcZH8zKMQCBAX14QEYDzIwMjMwMjE2MDUxMjEzWqCCArIwggKuMIICVKADA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vcNAQkDMQ0GCyqGSIb3DQEJEAEEMBwGCSqGSIb3DQEJBTEPFw0yMzAyMTYwNTEyMTNaMC8GCSqGSIb3DQEJBDEiBCDBJjxeHZSCyeAwwwRYn1AABHDFOwTHRrjfnKnKatew/jANBgkqgRzPVQGCLQEFAARHMEUCIQCjQnk6Tu+2Z5nu+EYZ1XDlSyqJLFxRq123VfXiQtDZ+gIgM5t+9VabEWpgt5abuCHVoYUEJajUdyXuansY0piH/hM==";

        String time = sedTss.getTSInfo(result, 1);
        String hash = sedTss.getTSInfo(result, 2);
        String signCert = sedTss.getTSInfo(result, 3);
        String ServeCertNum = sedTss.getTSInfo(result, 4);
        System.out.println("时间"+time);
        System.out.println("时间戳（原文）hash:"+hash);
        System.out.println("签名证书"+signCert);
        System.out.println("服务器证书序列号SN"+ServeCertNum);
        //验证时间戳
        int i = sedTss.verifyTS(result,null);
        System.out.println(i==1? "验证成功":"验证失败");


    }

    /**
     * 获取服务端证书
     */
    @Test
    public void getServerCertificate() throws SVSConnectException, ApplicationNotFoundException, InitException {
        String serverCertificate = sedTss.getServerCertificate();
                System.out.println("服务端证书"+serverCertificate);


    }

    @Test
    public void getServerCertificate2() throws SVSConnectException, ApplicationNotFoundException, InitException, ParameterTooLongException {
        String serverCertificate = sedTss.getCert("1b40000000000012d9c4");
        System.out.println("服务端证书"+serverCertificate);


    }
}
